package cn.sxgan.utils;

import java.lang.reflect.Field;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 公共方法工具类
 * @Author: sxgan
 * @Date: 2024-05-19 11:43
 * @Version: 1.0
 **/
public class CommonUtils {
    
    private static final String CHAR_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final SecureRandom RANDOM = new SecureRandom();
    
    /**
     * 生成0-9，A-Z之间的随机字符串
     *
     * @param length 指定字符串长度
     * @return 返回生成字符串
     */
    public static String generateRandomCode(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be positive");
        }
        StringBuilder code = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            code.append(CHAR_SET.charAt(RANDOM.nextInt(CHAR_SET.length())));
        }
        return code.toString();
    }
    
    /**
     * 比较两个对象指定属性值是否完全相等
     *
     * @param object1    对象1
     * @param object2    对象2
     * @param fieldNames 需要比较的字段名
     * @return true/false
     */
    public static boolean compareObjects(Object object1, Object object2, String[] fieldNames) {
        if (object1 == object2) {
            return true;
        }
        if (object1 == null || object2 == null) {
            return false;
        }
        if (!object1.getClass().equals(object2.getClass())) {
            return false;
        }
        
        Field[] fields = object1.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                for (String fieldName : fieldNames) {
                    if (field.getName().equals(fieldName)) {
                        field.setAccessible(true);
                        Object value1 = field.get(object1);
                        Object value2 = field.get(object2);
                        if (value1 == null && value2 != null) {
                            return false;
                        }
                        if (value1 != null && value2 == null) {
                            return false;
                        }
                        if (value1 == null && value2 == null) {
                            continue;
                        }
                        if (!value1.equals(value2)) {
                            return false;
                        }
                    }
                }
            }
            return true;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 获取随机UUID
     *
     * @return 不带间隔符的小写UUID
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "").toLowerCase();
    }
    
    /**
     * 将集合根据指定的数字n进行分块，分块尽量平均
     *
     * @param collection 原始集合
     * @param n          分块数量
     * @param <T>        集合元素类型
     * @return 包含n个子集合的列表
     */
    public static <T> List<List<T>> divideCollection(Collection<T> collection, int n) {
        if (n <= 0) {
            throw new IllegalArgumentException("分块数量n必须大于0");
        }
        
        List<List<T>> dividedLists = new ArrayList<>();
        int size = collection.size();
        
        if (size == 0) {
            return dividedLists;
        }
        
        // 计算每个子集合的基础大小和余数
        int baseSize = size / n;
        int remainder = size % n;
        
        // 创建n个空的子集合
        for (int i = 0; i < n; i++) {
            dividedLists.add(new ArrayList<>());
        }
        
        // 遍历原始集合，将元素依次添加到各个子集合中
        int index = 0;
        for (T element : collection) {
            // 计算当前元素应该放入的子集合索引
            int subListIndex = index % n;
            
            // 如果当前子集合已经达到了基础大小，并且还有余数未分配，则需要调整
            if (dividedLists.get(subListIndex).size() >= baseSize + (subListIndex < remainder ? 1 : 0)) {
                subListIndex = (subListIndex + 1) % n;
            }
            
            dividedLists.get(subListIndex).add(element);
            index++;
        }
        
        return dividedLists;
    }
    
    // public static void main(String[] args) {
    //     StopWatch stopWatch = new StopWatch();
    //     stopWatch.start();
    //     List<PersonInfo> originalList = new ArrayList<>();
    //     for (int i = 1; i <= 4; i++) {
    //         PersonInfo personInfoByID = IdCardRandom.getPersonInfoByID(IdCardRandom.generateRandomID());
    //         originalList.add(personInfoByID);
    //     }
    //
    //     List<List<PersonInfo>> dividedLists = divideCollection(originalList, 5);
    //
    //     for (int i = 0; i < dividedLists.size(); i++) {
    //         System.out.println("子集 " + (i + 1) + ": " + dividedLists.get(i));
    //     }
    //     stopWatch.stop();
    //     System.out.println(stopWatch.getTotalTimeMillis());
    // }
}



